package com.wen.controller;

import com.wen.pojo.StockDetails;
import com.wen.service.GatherDetailsService;
import com.wen.pojo.vo.ProductVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wen.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.Product;
import com.wen.pojo.dto.ProductAndStockDetails;
import com.wen.pojo.dto.ProductDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xie
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService  productService;

    @Autowired
    private GatherDetailsService gatherDetailsService;

    @GetMapping("/list")
    public R categoryHead(){
        List<Product> productList = productService.list();
        return R.ok().data("children",productList);
    }

    @PostMapping("page/{pageNum}/{pageSize}")
    public R page(@PathVariable Long pageNum,@PathVariable Long pageSize){
        Page<Product> productPage = productService.queryByPage(pageNum, pageSize);
        return R.ok().data("Supplier",productPage.getRecords()).
                data("total",productPage.getTotal()).
                data("current",productPage.getCurrent()).
                data("size",productPage.getSize());
    }

    @PostMapping("selectid/{id}")
    public R selectid(@PathVariable String id){

        Product byId = productService.getById(id);

        return R.ok().data("product",byId);
    }




    /**
     * 商品信息管理
     * 商品列表 (分页查询)
     * @param dto
     * @return
     */
    @PostMapping("/productPage")
    public R page(@RequestBody ProductDto dto,Integer pageNum,Integer size){
        Page<Product> medicinesPage = productService.queryPage(dto,pageNum,size);
        return R.ok().data("records",medicinesPage.getRecords())
                .data("total",medicinesPage.getTotal())
                .data("current",medicinesPage.getCurrent())
                .data("size",medicinesPage.getSize());

    }


    /**
     * 商品信息管理
     * 商品列表
     * 修改
     * @param product
     * @return
     */
    @PutMapping("/productUpdate")
    public  R updateProduct(@RequestBody Product product){
        boolean flag = productService.updateById(product);
        if (flag) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");

    }

    /**
     * 商品信息管理
     * 商品列表
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/productDelete/{id}")
    public R removeProduct(@PathVariable String id){
        boolean flag = productService.removeById(id);
        if (flag) {
            return R.ok().message("删除成功！");
        }
        return R.error().message("删除失败！");

    }



    /**
     * 商品信息管理
     * 商品列表
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/productGetId/{id}")
    public R getById(@PathVariable String id){
        Product selectById = productService.getById(id);
        String mainpictures = selectById.getMainpictures();
        String[] split = mainpictures.split(",");

        return R.ok().data("selectById",selectById).data("imageArray",split);
    }


    /**
     * 商品信息管理
     * 商品列表
     * 添加
     * @param product
     * @return
     */
    @PostMapping("/ProductAdd")
    public R addProduct(@RequestBody Product product){
        int flag = productService.getProductAdd(product);
        if (flag > 0) {
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    /**
     * 查询该商品的库存数量
     * @return
     */
    @GetMapping("/SelProductAndStockDetails/{id}")
    public R getSelProductAndStockDetails(@PathVariable String id){
        List<StockDetails> stockDetailsList = productService.getSelStockDetails(id);

        int countList = 0;
        if (stockDetailsList == null)
            return null;

        for (StockDetails stockDetailsOne:  stockDetailsList) {
               int countOne = (int) stockDetailsOne.getRealAmount();

               countList += countOne;

        }

        System.out.println(stockDetailsList);
        return R.ok().data("ProductStockDetails",stockDetailsList).data("countList",countList);
    }

    /**
     * 添加该商品的库存数量
     * @param stock
     * @param id
     * @return
     */
    @PutMapping("/UpdateStock/{stock}/{id}")
    public  R UpdateStock(@PathVariable String stock,@PathVariable String id ){
        int flag = productService.getUpdateProduct(stock,id);
        if (flag>0) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");

    }


    /**
     * 是否上下架
     * @param id
     * @param onff
     * @return
     */
    @PutMapping("/UpdateOnff/{id}/{onff}")
    public R UpdateOnff(@PathVariable String id,
                            @PathVariable Integer onff){
        int updateSelected = productService.getUpdateOnff(id,onff);

        if (updateSelected > 0) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");
    }


    @PutMapping("/UpdateAmount/{amount}/{id}")
    public R UpdateAmount(@PathVariable String id,
                        @PathVariable String amount){
        int updateSelected = gatherDetailsService.getUpdateAmount(id,amount);

        if (updateSelected > 0) {
            return R.ok().message("修改成功！");
        }
        return R.error().message("修改失败！");

    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public boolean del(@RequestBody List<String> ids) {
        return productService.removeByIds(ids);
    }


    @PostMapping("/page/{pageNum}/{pageSize}")
    public R page(@RequestBody ProductVo dto, @PathVariable Long pageNum, @PathVariable Long pageSize){
        Page<Product> medicinesPage = productService.pageListMedicinesCondition(dto, pageNum, pageSize);
        return R.ok().data("files",medicinesPage.getRecords()).data("total",medicinesPage.getTotal())
                .data("current",medicinesPage.getCurrent()).data("size",medicinesPage.getSize());
    }



}
